from apps import db


# 用户信息模型类
class Student(db.Model):
    __tablename__ = 't_student'

    stu_id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=True)
    account_id = db.Column(db.Integer, db.ForeignKey('t_account.account_id'), unique=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    name = db.Column(db.String(80), nullable=False)
    sex = db.Column(db.Integer, nullable=False)
    birth_date = db.Column(db.Date, nullable=False)
    address = db.Column(db.String(80), nullable=False)
    college = db.Column(db.String(80), nullable=False)
    major = db.Column(db.String(80), nullable=False)
    credits = db.Column(db.Integer, nullable=False)
    GPA = db.Column(db.Float, nullable=False)   # 绩点

    def to_dict(self):
        return {
            'stu_id': self.stu_id,
            'account_id': self.account_id,
            'username': self.username,
            'name': self.name,
            'birth_date': self.birth_date.isoformat() if self.birth_date else None,
            'address': self.address,
            'sex': self.sex,
        }

    def __repr__(self):
        return 'stu_id: {}, username: {}, name: {}, sex: {},'.format(
            self.stu_id, self.username, self.name, self.sex)